home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -serious- / programming / other / mesa / mesa-glut / src-glut.aos / glutshapes.c < prev    next >
C/C++ Source or Header  |  2000-02-23  |  14KB  |  578 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1994, 1997. */
  3.  
  4. /**
  5. (c) Copyright 1993, Silicon Graphics, Inc.
  6.  
  7. ALL RIGHTS RESERVED
  8.  
  9. Permission to use, copy, modify, and distribute this software
  10. for any purpose and without fee is hereby granted, provided
  11. that the above copyright notice appear in all copies and that
  12. both the copyright notice and this permission notice appear in
  13. supporting documentation, and that the name of Silicon
  14. Graphics, Inc. not be used in advertising or publicity
  15. pertaining to distribution of the software without specific,
  16. written prior permission.
  17.  
  18. THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU
  19. "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR
  20. OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
  21. MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  IN NO
  22. EVENT SHALL SILICON GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE
  23. ELSE FOR ANY DIRECT, SPECIAL, INCIDENTAL, INDIRECT OR
  24. CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER,
  25. INCLUDING WITHOUT LIMITATION, LOSS OF PROFIT, LOSS OF USE,
  26. SAVINGS OR REVENUE, OR THE CLAIMS OF THIRD PARTIES, WHETHER OR
  27. NOT SILICON GRAPHICS, INC.  HAS BEEN ADVISED OF THE POSSIBILITY
  28. OF SUCH LOSS, HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. ARISING OUT OF OR IN CONNECTION WITH THE POSSESSION, USE OR
  30. PERFORMANCE OF THIS SOFTWARE.
  31.  
  32. US Government Users Restricted Rights
  33.  
  34. Use, duplication, or disclosure by the Government is subject to
  35. restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  36. (c)(1)(ii) of the Rights in Technical Data and Computer
  37. Software clause at DFARS 252.227-7013 and/or in similar or
  38. successor clauses in the FAR or the DOD or NASA FAR
  39. Supplement.  Unpublished-- rights reserved under the copyright
  40. laws of the United States.  Contractor/manufacturer is Silicon
  41. Graphics, Inc., 2011 N.  Shoreline Blvd., Mountain View, CA
  42. 94039-7311.
  43.  
  44. OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  45. */
  46.  
  47. /*
  48.  * glutShapes.c
  49.  *
  50.  * Modified  27 Jun 1998
  51.  * by Jarno van der Linden
  52.  * jarno@kcbbs.gen.nz
  53.  *
  54.  * Based on glut_shapes.c
  55.  * Some changes to work with Amiga GLUT
  56.  *
  57.  */
  58.  
  59. #include <math.h>
  60.  
  61. #include "glutstuff.h"
  62.  
  63. /* Some <math.h> files do not define M_PI... */
  64. #ifndef M_PI
  65. #define M_PI 3.14159265358979323846
  66. #endif
  67.  
  68. static GLUquadricObj *quadObj;
  69.  
  70. #define QUAD_OBJ_INIT() { if(!quadObj) initQuadObj(); }
  71.  
  72. static void initQuadObj(void)
  73. {
  74.   quadObj = gluNewQuadric();
  75. //  if (!quadObj)
  76.   //    __glutFatalError("out of memory.");
  77. }
  78.  
  79. /* CENTRY */
  80. void glutWireSphere(GLdouble radius, GLint slices, GLint stacks)
  81. {
  82.   QUAD_OBJ_INIT();
  83.   gluQuadricDrawStyle(quadObj, GLU_LINE);
  84.   gluQuadricNormals(quadObj, GLU_SMOOTH);
  85.   /* If we ever changed/used the texture or orientation state
  86.    * of quadObj, we'd need to change it to the defaults here
  87.    * with gluQuadricTexture and/or gluQuadricOrientation. */
  88.   gluSphere(quadObj, radius, slices, stacks);
  89. }
  90.  
  91. void glutSolidSphere(GLdouble radius, GLint slices, GLint stacks)
  92. {
  93.   QUAD_OBJ_INIT();
  94.   gluQuadricDrawStyle(quadObj, GLU_FILL);
  95.   gluQuadricNormals(quadObj, GLU_SMOOTH);
  96.   /* If we ever changed/used the texture or orientation state
  97.    * of quadObj, we'd need to change it to the defaults here
  98.    * with gluQuadricTexture and/or gluQuadricOrientation. */
  99.   gluSphere(quadObj, radius, slices, stacks);
  100. }
  101.  
  102. void glutWireCone(GLdouble base, GLdouble height, GLint slices, GLint stacks)
  103. {
  104.   QUAD_OBJ_INIT();
  105.   gluQuadricDrawStyle(quadObj, GLU_LINE);
  106.   gluQuadricNormals(quadObj, GLU_SMOOTH);
  107.   /* If we ever changed/used the texture or orientation state
  108.    * of quadObj, we'd need to change it to the defaults here
  109.    * with gluQuadricTexture and/or gluQuadricOrientation. */
  110.   gluCylinder(quadObj, base, 0.0, height, slices, stacks);
  111. }
  112.  
  113. void glutSolidCone(GLdouble base, GLdouble height, GLint slices, GLint stacks)
  114. {
  115.   QUAD_OBJ_INIT();
  116.   gluQuadricDrawStyle(quadObj, GLU_FILL);
  117.   gluQuadricNormals(quadObj, GLU_SMOOTH);
  118.   /* If we ever changed/used the texture or orientation state
  119.    * of quadObj, we'd need to change it to the defaults here
  120.    * with gluQuadricTexture and/or gluQuadricOrientation. */
  121.   gluCylinder(quadObj, base, 0.0, height, slices, stacks);
  122. }
  123.  
  124. /* ENDCENTRY */
  125.  
  126. static void drawBox(GLfloat size, GLenum type)
  127. {
  128.   static GLfloat n[6][3] =
  129.   {
  130.     {-1.0, 0.0, 0.0},
  131.     {0.0, 1.0, 0.0},
  132.     {1.0, 0.0, 0.0},
  133.     {0.0, -1.0, 0.0},
  134.     {0.0, 0.0, 1.0},
  135.     {0.0, 0.0, -1.0}
  136.   };
  137.   static GLint faces[6][4] =
  138.   {
  139.     {0, 1, 2, 3},
  140.     {3, 2, 6, 7},
  141.     {7, 6, 5, 4},
  142.     {4, 5, 1, 0},
  143.     {5, 6, 2, 1},
  144.     {7, 4, 0, 3}
  145.   };
  146.   GLfloat v[8][3];
  147.   GLint i;
  148.  
  149.   v[0][0] = v[1][0] = v[2][0] = v[3][0] = -size / 2;
  150.   v[4][0] = v[5][0] = v[6][0] = v[7][0] = size / 2;
  151.   v[0][1] = v[1][1] = v[4][1] = v[5][1] = -size / 2;
  152.   v[2][1] = v[3][1] = v[6][1] = v[7][1] = size / 2;
  153.   v[0][2] = v[3][2] = v[4][2] = v[7][2] = -size / 2;
  154.   v[1][2] = v[2][2] = v[5][2] = v[6][2] = size / 2;
  155.  
  156.   for (i = 5; i >= 0; i--) {
  157.     glBegin(type);
  158.     glNormal3fv(&n[i][0]);
  159.     glVertex3fv(&v[faces[i][0]][0]);
  160.     glVertex3fv(&v[faces[i][1]][0]);
  161.     glVertex3fv(&v[faces[i][2]][0]);
  162.     glVertex3fv(&v[faces[i][3]][0]);
  163.     glEnd();
  164.   }
  165. }
  166.  
  167. /* CENTRY */
  168. void glutWireCube(GLdouble size)
  169. {
  170.   drawBox(size, GL_LINE_LOOP);
  171. }
  172.  
  173. void glutSolidCube(GLdouble size)
  174. {
  175.   drawBox(size, GL_QUADS);
  176. }
  177.  
  178. /* ENDCENTRY */
  179.  
  180. static void doughnut(GLfloat r, GLfloat R, GLint nsides, GLint rings)
  181. {
  182.   int i, j;
  183.   GLfloat theta, phi, theta1;
  184.   GLfloat cosTheta, sinTheta;
  185.   GLfloat cosTheta1, sinTheta1;
  186.   GLfloat ringDelta, sideDelta;
  187.  
  188.   ringDelta = 2.0 * M_PI / rings;
  189.   sideDelta = 2.0 * M_PI / nsides;
  190.  
  191.   theta = 0.0;
  192.   cosTheta = 1.0;
  193.   sinTheta = 0.0;
  194.   for (i = rings - 1; i >= 0; i--) {
  195.     theta1 = theta + ringDelta;
  196.     cosTheta1 = cos(theta1);
  197.     sinTheta1 = sin(theta1);
  198.     glBegin(GL_QUAD_STRIP);
  199.     phi = 0.0;
  200.     for (j = nsides; j >= 0; j--) {
  201.       GLfloat cosPhi, sinPhi, dist;
  202.  
  203.       phi += sideDelta;
  204.       cosPhi = cos(phi);
  205.       sinPhi = sin(phi);
  206.       dist = R + r * cosPhi;
  207.  
  208.       glNormal3f(cosTheta1 * cosPhi, -sinTheta1 * cosPhi, sinPhi);
  209.       glVertex3f(cosTheta1 * dist, -sinTheta1 * dist, r * sinPhi);
  210.       glNormal3f(cosTheta * cosPhi, -sinTheta * cosPhi, sinPhi);
  211.       glVertex3f(cosTheta * dist, -sinTheta * dist, r * sinPhi);
  212.     }
  213.     glEnd();
  214.     theta = theta1;
  215.     cosTheta = cosTheta1;
  216.     sinTheta = sinTheta1;
  217.   }
  218. }
  219.  
  220. /* CENTRY */
  221. void glutWireTorus(GLdouble innerRadius, GLdouble outerRadius,
  222.            GLint nsides, GLint rings)
  223. {
  224.   glPushAttrib(GL_POLYGON_BIT);
  225.   glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  226.   doughnut(innerRadius, outerRadius, nsides, rings);
  227.   glPopAttrib();
  228. }
  229.  
  230. void glutSolidTorus(GLdouble innerRadius, GLdouble outerRadius,
  231.             GLint nsides, GLint rings)
  232. {
  233.   doughnut(innerRadius, outerRadius, nsides, rings);
  234. }
  235.  
  236. /* ENDCENTRY */
  237.  
  238. static GLfloat dodec[20][3];
  239.  
  240. static void initDodecahedron(void)
  241. {
  242.   GLfloat alpha, beta;
  243.  
  244.   alpha = sqrt(2.0 / (3.0 + sqrt(5.0)));
  245.   beta = 1.0 + sqrt(6.0 / (3.0 + sqrt(5.0)) -
  246.             2.0 + 2.0 * sqrt(2.0 / (3.0 + sqrt(5.0))));
  247.   /* *INDENT-OFF* */
  248.   dodec[0][0] = -alpha; dodec[0][1] = 0; dodec[0][2] = beta;
  249.   dodec[1][0] = alpha; dodec[1][1] = 0; dodec[1][2] = beta;
  250.   dodec[2][0] = -1; dodec[2][1] = -1; dodec[2][2] = -1;
  251.   dodec[3][0] = -1; dodec[3][1] = -1; dodec[3][2] = 1;
  252.   dodec[4][0] = -1; dodec[4][1] = 1; dodec[4][2] = -1;
  253.   dodec[5][0] = -1; dodec[5][1] = 1; dodec[5][2] = 1;
  254.   dodec[6][0] = 1; dodec[6][1] = -1; dodec[6][2] = -1;
  255.   dodec[7][0] = 1; dodec[7][1] = -1; dodec[7][2] = 1;
  256.   dodec[8][0] = 1; dodec[8][1] = 1; dodec[8][2] = -1;
  257.   dodec[9][0] = 1; dodec[9][1] = 1; dodec[9][2] = 1;
  258.   dodec[10][0] = beta; dodec[10][1] = alpha; dodec[10][2] = 0;
  259.   dodec[11][0] = beta; dodec[11][1] = -alpha; dodec[11][2] = 0;
  260.   dodec[12][0] = -beta; dodec[12][1] = alpha; dodec[12][2] = 0;
  261.   dodec[13][0] = -beta; dodec[13][1] = -alpha; dodec[13][2] = 0;
  262.   dodec[14][0] = -alpha; dodec[14][1] = 0; dodec[14][2] = -beta;
  263.   dodec[15][0] = alpha; dodec[15][1] = 0; dodec[15][2] = -beta;
  264.   dodec[16][0] = 0; dodec[16][1] = beta; dodec[16][2] = alpha;
  265.   dodec[17][0] = 0; dodec[17][1] = beta; dodec[17][2] = -alpha;
  266.   dodec[18][0] = 0; dodec[18][1] = -beta; dodec[18][2] = alpha;
  267.   dodec[19][0] = 0; dodec[19][1] = -beta; dodec[19][2] = -alpha;
  268.   /* *INDENT-ON* */
  269.  
  270. }
  271.  
  272. #define DIFF3(_a,_b,_c) { \
  273.     (_c)[0] = (_a)[0] - (_b)[0]; \
  274.     (_c)[1] = (_a)[1] - (_b)[1]; \
  275.     (_c)[2] = (_a)[2] - (_b)[2]; \
  276. }
  277.  
  278. static void crossprod(GLfloat v1[3], GLfloat v2[3], GLfloat prod[3])
  279. {
  280.   GLfloat p[3];                            /* in case prod == v1 or v2 */
  281.  
  282.   p[0] = v1[1] * v2[2] - v2[1] * v1[2];
  283.   p[1] = v1[2] * v2[0] - v2[2] * v1[0];
  284.   p[2] = v1[0] * v2[1] - v2[0] * v1[1];
  285.   prod[0] = p[0];
  286.   prod[1] = p[1];
  287.   prod[2] = p[2];
  288. }
  289.  
  290. static void normalize(GLfloat v[3])
  291. {
  292.   GLfloat d;
  293.  
  294.   d = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
  295.   if (d == 0.0) {
  296. //    __glutWarning("normalize: zero length vector");
  297.     v[0] = d = 1.0;
  298.   }
  299.   d = 1 / d;
  300.   v[0] *= d;
  301.   v[1] *= d;
  302.   v[2] *= d;
  303. }
  304.  
  305. static void pentagon(int a, int b, int c, int d, int e, GLenum shadeType)
  306. {
  307.   GLfloat n0[3], d1[3], d2[3];
  308.  
  309.   DIFF3(dodec[a], dodec[b], d1);
  310.   DIFF3(dodec[b], dodec[c], d2);
  311.   crossprod(d1, d2, n0);
  312.   normalize(n0);
  313.  
  314.   glBegin(shadeType);
  315.   glNormal3fv(n0);
  316.   glVertex3fv(&dodec[a][0]);
  317.   glVertex3fv(&dodec[b][0]);
  318.   glVertex3fv(&dodec[c][0]);
  319.   glVertex3fv(&dodec[d][0]);
  320.   glVertex3fv(&dodec[e][0]);
  321.   glEnd();
  322. }
  323.  
  324. static void dodecahedron(GLenum type)
  325. {
  326.   static int inited = 0;
  327.  
  328.   if (inited == 0) {
  329.     inited = 1;
  330.     initDodecahedron();
  331.   }
  332.   pentagon(0, 1, 9, 16, 5, type);
  333.   pentagon(1, 0, 3, 18, 7, type);
  334.   pentagon(1, 7, 11, 10, 9, type);
  335.   pentagon(11, 7, 18, 19, 6, type);
  336.   pentagon(8, 17, 16, 9, 10, type);
  337.   pentagon(2, 14, 15, 6, 19, type);
  338.   pentagon(2, 13, 12, 4, 14, type);
  339.   pentagon(2, 19, 18, 3, 13, type);
  340.   pentagon(3, 0, 5, 12, 13, type);
  341.   pentagon(6, 15, 8, 10, 11, type);
  342.   pentagon(4, 17, 8, 15, 14, type);
  343.   pentagon(4, 12, 5, 16, 17, type);
  344. }
  345.  
  346. /* CENTRY */
  347. void glutWireDodecahedron(void)
  348. {
  349.   dodecahedron(GL_LINE_LOOP);
  350. }
  351.  
  352. void glutSolidDodecahedron(void)
  353. {
  354.   dodecahedron(GL_TRIANGLE_FAN);
  355. }
  356.  
  357. /* ENDCENTRY */
  358.  
  359. static void recorditem(GLfloat * n1, GLfloat * n2, GLfloat * n3,
  360.                GLenum shadeType)
  361. {
  362.   GLfloat q0[3], q1[3];
  363.  
  364.   DIFF3(n1, n2, q0);
  365.   DIFF3(n2, n3, q1);
  366.   crossprod(q0, q1, q1);
  367.   normalize(q1);
  368.  
  369.   glBegin(shadeType);
  370.   glNormal3fv(q1);
  371.   glVertex3fv(n1);
  372.   glVertex3fv(n2);
  373.   glVertex3fv(n3);
  374.   glEnd();
  375. }
  376.  
  377. static void subdivide(GLfloat * v0, GLfloat * v1, GLfloat * v2,
  378.               GLenum shadeType)
  379. {
  380.   int depth;
  381.   GLfloat w0[3], w1[3], w2[3];
  382.   GLfloat l;
  383.   int i, j, k, n;
  384.  
  385.   depth = 1;
  386.   for (i = 0; i < depth; i++) {
  387.     for (j = 0; i + j < depth; j++) {
  388.       k = depth - i - j;
  389.       for (n = 0; n < 3; n++) {
  390.     w0[n] = (i * v0[n] + j * v1[n] + k * v2[n]) / depth;
  391.     w1[n] = ((i + 1) * v0[n] + j * v1[n] + (k - 1) * v2[n])
  392.       / depth;
  393.     w2[n] = (i * v0[n] + (j + 1) * v1[n] + (k - 1) * v2[n])
  394.       / depth;
  395.       }
  396.       l = sqrt(w0[0] * w0[0] + w0[1] * w0[1] + w0[2] * w0[2]);
  397.       w0[0] /= l;
  398.       w0[1] /= l;
  399.       w0[2] /= l;
  400.       l = sqrt(w1[0] * w1[0] + w1[1] * w1[1] + w1[2] * w1[2]);
  401.       w1[0] /= l;
  402.       w1[1] /= l;
  403.       w1[2] /= l;
  404.       l = sqrt(w2[0] * w2[0] + w2[1] * w2[1] + w2[2] * w2[2]);
  405.       w2[0] /= l;
  406.       w2[1] /= l;
  407.       w2[2] /= l;
  408.       recorditem(w1, w0, w2, shadeType);
  409.     }
  410.   }
  411. }
  412.  
  413. static void drawtriangle(int i, GLfloat data[][3], int ndx[][3],
  414.              GLenum shadeType)
  415. {
  416.   GLfloat *x0, *x1, *x2;
  417.  
  418.   x0 = data[ndx[i][0]];
  419.   x1 = data[ndx[i][1]];
  420.   x2 = data[ndx[i][2]];
  421.   subdivide(x0, x1, x2, shadeType);
  422. }
  423.  
  424. /* octahedron data: The octahedron produced is centered at the
  425.  * origin and has radius 1.0 */
  426. static GLfloat odata[6][3] =
  427. {
  428.   {1.0, 0.0, 0.0},
  429.   {-1.0, 0.0, 0.0},
  430.   {0.0, 1.0, 0.0},
  431.   {0.0, -1.0, 0.0},
  432.   {0.0, 0.0, 1.0},
  433.   {0.0, 0.0, -1.0}
  434. };
  435.  
  436. static int ondex[8][3] =
  437. {
  438.   {0, 4, 2},
  439.   {1, 2, 4},
  440.   {0, 3, 4},
  441.   {1, 4, 3},
  442.   {0, 2, 5},
  443.   {1, 5, 2},
  444.   {0, 5, 3},
  445.   {1, 3, 5}
  446. };
  447.  
  448. static void octahedron(GLenum shadeType)
  449. {
  450.   int i;
  451.  
  452.   for (i = 7; i >= 0; i--) {
  453.     drawtriangle(i, odata, ondex, shadeType);
  454.   }
  455. }
  456.  
  457. /* CENTRY */
  458. void glutWireOctahedron(void)
  459. {
  460.   octahedron(GL_LINE_LOOP);
  461. }
  462.  
  463. void glutSolidOctahedron(void)
  464. {
  465.   octahedron(GL_TRIANGLES);
  466. }
  467.  
  468. /* ENDCENTRY */
  469.  
  470. /* icosahedron data: These numbers are rigged to make an
  471.  * icosahedron of radius 1.0 */
  472.  
  473. #define X .525731112119133606
  474. #define Z .850650808352039932
  475.  
  476. static GLfloat idata[12][3] =
  477. {
  478.   {-X, 0, Z},
  479.   {X, 0, Z},
  480.   {-X, 0, -Z},
  481.   {X, 0, -Z},
  482.   {0, Z, X},
  483.   {0, Z, -X},
  484.   {0, -Z, X},
  485.   {0, -Z, -X},
  486.   {Z, X, 0},
  487.   {-Z, X, 0},
  488.   {Z, -X, 0},
  489.   {-Z, -X, 0}
  490. };
  491.  
  492. static int indexes[20][3] =
  493. {
  494.   {0, 4, 1},
  495.   {0, 9, 4},
  496.   {9, 5, 4},
  497.   {4, 5, 8},
  498.   {4, 8, 1},
  499.   {8, 10, 1},
  500.   {8, 3, 10},
  501.   {5, 3, 8},
  502.   {5, 2, 3},
  503.   {2, 7, 3},
  504.   {7, 10, 3},
  505.   {7, 6, 10},
  506.   {7, 11, 6},
  507.   {11, 0, 6},
  508.   {0, 1, 6},
  509.   {6, 1, 10},
  510.   {9, 0, 11},
  511.   {9, 11, 2},
  512.   {9, 2, 5},
  513.   {7, 2, 11},
  514. };
  515.  
  516. static void icosahedron(GLenum shadeType)
  517. {
  518.   int i;
  519.  
  520.   for (i = 19; i >= 0; i--) {
  521.     drawtriangle(i, idata, indexes, shadeType);
  522.   }
  523. }
  524.  
  525. /* CENTRY */
  526. void glutWireIcosahedron(void)
  527. {
  528.   icosahedron(GL_LINE_LOOP);
  529. }
  530.  
  531. void glutSolidIcosahedron(void)
  532. {
  533.   icosahedron(GL_TRIANGLES);
  534. }
  535.  
  536. /* ENDCENTRY */
  537.  
  538. /* tetrahedron data: */
  539.  
  540. #define T       1.73205080756887729
  541.  
  542. static GLfloat tdata[4][3] =
  543. {
  544.   {T, T, T},
  545.   {T, -T, -T},
  546.   {-T, T, -T},
  547.   {-T, -T, T}
  548. };
  549.  
  550. static int tndex[4][3] =
  551. {
  552.   {0, 1, 3},
  553.   {2, 1, 0},
  554.   {3, 2, 0},
  555.   {1, 2, 3}
  556. };
  557.  
  558. static void tetrahedron(GLenum shadeType)
  559. {
  560.   int i;
  561.  
  562.   for (i = 3; i >= 0; i--)
  563.     drawtriangle(i, tdata, tndex, shadeType);
  564. }
  565.  
  566. /* CENTRY */
  567. void glutWireTetrahedron(void)
  568. {
  569.   tetrahedron(GL_LINE_LOOP);
  570. }
  571.  
  572. void glutSolidTetrahedron(void)
  573. {
  574.   tetrahedron(GL_TRIANGLES);
  575. }
  576.  
  577. /* ENDCENTRY */
  578.